package club.justwill.single.persistence.jpo;

import club.justwill.single.persistence.support.InvatationPK;
import club.justwill.single.persistence.support.InvitationBase;

import javax.persistence.*;
import java.io.Serializable;

/**
 * Created by luoyu on 15-12-23.
 */
@Entity
@Table(name = "invitation")
@IdClass(InvatationPK.class)
@NamedQueries({
        @NamedQuery(name = "Invitation.getByInviterIdAndInviteeId",
                query = "select inv from Invitation inv where inv.inviterId = :inviterId and inv.inviteeId = :inviteeId"),
        @NamedQuery(name = "Invitation.searchByInviterId",
                query = "select inv from Invitation inv where inv.inviterId = :inviterId"),
        @NamedQuery(name = "Invitation.getInviteeIdCount",
                query = "select count(invit.inviterId) from Invitation invit where invit.inviterId = :inviterId and invit.inviterId != inviteeId")
})
public class Invitation extends InvitationBase {

    @Id
    @Column(name = "inviter_id")
    private long inviterId;

    @Id
    @Column(name = "invitee_id")
    private long inviteeId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inviter_id", referencedColumnName = "user_id", insertable = false, updatable = false)
    private User inviter;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "invitee_id", referencedColumnName = "user_id", insertable = false, updatable = false)
    private User invitee;

    public User getInviter() {
        return inviter;
    }

    public void setInviter(User inviter) {
        this.inviter = inviter;
    }

    public User getInvitee() {
        return invitee;
    }

    public void setInvitee(User invitee) {
        this.invitee = invitee;
    }

    public long getInviterId() {
        return inviterId;
    }

    public void setInviterId(Long inviterId) {
        if(inviterId == null) {
            this.inviteeId = 0L;
        } else {
            this.inviterId = inviterId;
        }
    }

    public long getInviteeId() {
        return inviteeId;
    }

    public void setInviteeId(Long inviteeId) {
        if(inviteeId == null) {
            this.inviteeId = 0L;
        } else {
            this.inviteeId = inviteeId;
        }
    }
}
